package com.lhf.azkaban.springbatch.example.batchconfig;

import com.lhf.azkaban.springbatch.example.batchprocessing.RepayOrderProcessor;
import com.lhf.azkaban.springbatch.example.batchreader.RepayOrderFlatFileItemReader;
import com.lhf.azkaban.springbatch.example.batchwriter.RepayOrderFlatFileItemWriter;
import com.lhf.azkaban.springbatch.example.batchwriter.RepayOrderMybatisItemWrite;
import com.lhf.azkaban.springbatch.example.batchwriter.RepayOrderMybatisWrite;
import com.lhf.azkaban.springbatch.example.job.listener.MyJobListener;
import com.lhf.azkaban.springbatch.example.mode.RepayOrderInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.launch.support.SimpleJobOperator;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import javax.batch.operations.JobOperator;

/**
 * @Author:95780
 * @Date: 14:19 2019/11/21
 * @Description:
 */
//@Configuration
//@EnableBatchProcessing
//@StepScope
public class RepayOrderConfig { // extends BaseConfig implements ApplicationContextAware {
    public static final String REPAY_ORDER_JOB = "repayOrderJob";
    public static final String REPAY_ORDER_JOB_START = "repayOrderJobStart";
    public static final String REPAY_ORDER_STEP = "repayOrderStep";
    @Value("${repay.orders.chunksize}")
    private int chunksize;
    //job由joblauncher提供；
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private JobRegistry jobRegistry;
    @Autowired
    private JobRepository jobRepository;

    @Autowired
    protected StepBuilderFactory stepBuilderFactory;


    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private JobExplorer jobExplorer;
    private ApplicationContext context;

    private static final Logger logger = LogManager.getLogger(RepayOrderConfig.class);

    @Autowired
    MyJobListener myJobListener;

//    @Autowired
//    private RepayOrderStepListener repayOrderStepListener;

    @Autowired
    private RepayOrderFlatFileItemReader repayOrderFlatFileItemReader;
    @Bean
    public RepayOrderProcessor repayOrderProcessor() {
        return new RepayOrderProcessor();
    }

    @Autowired
    private RepayOrderMybatisItemWrite repayOrderMybatisItemWrite;

    @Autowired
    private RepayOrderFlatFileItemWriter repayOrderFlatFileItemWriter;
    @Autowired
    private RepayOrderMybatisWrite       repayOrderMybatisWrite;
    @Bean
    public JobRegistryBeanPostProcessor jobRegistry() throws Exception {
        JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
        jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
        jobRegistryBeanPostProcessor.setBeanFactory(context.getAutowireCapableBeanFactory());
        jobRegistryBeanPostProcessor.afterPropertiesSet();
        return jobRegistryBeanPostProcessor;
    }

    @Bean(name = REPAY_ORDER_JOB)
    public Job repayOrderRouteJob() {
        return jobBuilderFactory.get(REPAY_ORDER_JOB)
                .incrementer(new RunIdIncrementer())
                //增加对job的监听
                .listener(myJobListener)
                .flow(repayOrderRouteStep())
                .end()
                .build();
    }
    @Bean(name = REPAY_ORDER_JOB_START)
    public Job repayOrderRouteJobStart() {
        return jobBuilderFactory.get(REPAY_ORDER_JOB_START)
                .incrementer(new RunIdIncrementer())
                //增加对job的监听
                .listener(myJobListener)
                .start(repayOrderRouteStep())
                .build();
    }
    @Bean
    public JobOperator jobOperator(){
        SimpleJobOperator operator = new SimpleJobOperator();
        operator.setJobLauncher(jobLauncher);
        operator.setJobParametersConverter(new DefaultJobParametersConverter());
        operator.setJobRepository(jobRepository);
        operator.setJobExplorer(jobExplorer);
        operator.setJobRegistry(jobRegistry);
        return (JobOperator) operator;
    }

    @Bean(name = REPAY_ORDER_STEP)
    public Step repayOrderRouteStep() {
        return stepBuilderFactory.get(REPAY_ORDER_STEP)
                //添加对step的监听，可以实现多step的所有监听
//                .listener(repayOrderStepListener)
                //一次性读取多少，提交多少
                .<RepayOrderInfo, RepayOrderInfo>chunk(chunksize)
                //跳过机制是针对每个执行线程设置，最大跳过数量为：skipLimit * throttleLimit
//                .faultTolerant().skipLimit(9)
                //reader
//                .skip(FlatFileParseException.class)
                //processor 自定义异常
//                .skip(MySkipException.class)
                .reader(repayOrderFlatFileItemReader)
                .processor(repayOrderProcessor())
                .writer(repayOrderMybatisWrite)
                //.writer(repayOrderMultiResourceFlatFileItemWriter)
                //.writer(repayOrderClassifierCompositeItemWriter)  //路由writer
                //.stream(repayOrderFlatFileItemWriter)  //路由writer stream 必须添加些文件的writer
                //.stream(repayOrderNewFlatFileItemWriter)
                //.stream(repayOrderMultiResourceFlatFileItemWriter)
                //.stream(repayOrderNewMultiResourceFlatFileItemWriter)
                //开启异步多线程  需要注意readers 和writers是否允许多线程
//                .taskExecutor(new SyncTaskExecutor())
//                .throttleLimit(10)
                .build();
    }

/*
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
*/



}
